package com.ymatou.iapi.godmch.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.createagentBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.setAgentRateBean;
import com.ymatou.iapi.godmch.parameter.setAgentRateBean.agentRate;
import com.ymatou.iapi.godmch.service.setAgentRateCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.agent_type;
import com.ymttest.database.sqlwapper.godmch.agent_typeWapper;
/**
*设置代理类型费率
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_setAgentRate{
	private static setAgentRateBean bean;
	private static setAgentRateCall call;
	public agent_typeWapper agentTypeWapper = new agent_typeWapper();
	
	private final static Integer channlNoAgent=500400810;//渠道无代理
	private final static Integer channlAndAgent=500400812;//渠道有代理
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("设置代理类型费率");
	}

	@Before
	public void caseUp() {
		bean=new setAgentRateBean();
		call=new setAgentRateCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_setAgentRate_001() {
		Logger.start(true,"正例-设置代理类型费率-代理-渠道是白名单 且 无代理商-设置1个代理类型费率");
		try {
			// 获取代理
			Map<String, Object> map = new HashMap();
			map.put("mchId", channlNoAgent);
			List<agent_type> list = agentTypeWapper.selectby(map);
			
			agentRate rate= (new setAgentRateBean()).new agentRate();
			List<agentRate> agentRates=new ArrayList();
			
			rate.setAgentType(list.get(0).getAgentType());
			rate.setAgentRate(3);
			agentRates.add(rate);
			
			bean.setMchId(channlNoAgent);
			bean.setAgentRateList(agentRates);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");
			list = agentTypeWapper.selectby(map);
			Logger.verifyEquals(rate.getAgentRate().toString(), list.get(0).getRate().stripTrailingZeros().toPlainString(), "验证agent_type表中的rate");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_setAgentRate_002() {
		Logger.start(true,"正例-设置代理类型费率-代理-渠道是白名单 且 无代理商-设置2个代理类型费率");
		try {
			
			// 获取代理
			Map<String, Object> map = new HashMap();
			map.put("mchId", channlNoAgent);
			List<agent_type> list = agentTypeWapper.selectby(map);
			
			List<agentRate> agentRates=new ArrayList();
			agentRate rate1= (new setAgentRateBean()).new agentRate();
			rate1.setAgentType(list.get(0).getAgentType());
			rate1.setAgentRate(3);
			agentRates.add(rate1);
			
			agentRate rate2= (new setAgentRateBean()).new agentRate();
			rate2.setAgentType(list.get(1).getAgentType());
			rate2.setAgentRate(4);
			agentRates.add(rate2);
			
			bean.setMchId(channlNoAgent);
			bean.setAgentRateList(agentRates);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");
			list = agentTypeWapper.selectby(map);
			Logger.verifyEquals(rate1.getAgentRate().toString(), list.get(0).getRate().stripTrailingZeros().toPlainString(), "验证第一个代理类型agent_type表中的rate");
			Logger.verifyEquals(rate2.getAgentRate().toString(), list.get(1).getRate().stripTrailingZeros().toPlainString(), "验证第二个代理类型agent_type表中的rate");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_setAgentRate_003() {
		Logger.start(true,"正例-设置代理类型费率-代理-渠道是白名单 且 有代理商-设置1个代理类型费率");
		try {
			// 获取代理
			Map<String, Object> map = new HashMap();
			map.put("mchId", channlAndAgent);
			List<agent_type> list = agentTypeWapper.selectby(map);
			
			agentRate rate= (new setAgentRateBean()).new agentRate();
			List<agentRate> agentRates=new ArrayList();
			
			rate.setAgentType(list.get(0).getAgentType());
			rate.setAgentRate(3);
			agentRates.add(rate);
			
			bean.setMchId(channlAndAgent);
			bean.setAgentRateList(agentRates);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");
			list = agentTypeWapper.selectby(map);
			Logger.verifyEquals(rate.getAgentRate().toString(), list.get(0).getRate().stripTrailingZeros().toPlainString(), "验证agent_type表中的rate");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_setAgentRate_004() {
		Logger.start(true,"正例-设置代理类型费率-代理-渠道是白名单 且 有代理商-设置2个代理类型费率");
		try {
			// 获取代理
			Map<String, Object> map = new HashMap();
			map.put("mchId", channlAndAgent);
			List<agent_type> list = agentTypeWapper.selectby(map);
			
			List<agentRate> agentRates=new ArrayList();
			agentRate rate1= (new setAgentRateBean()).new agentRate();
			rate1.setAgentType(list.get(0).getAgentType());
			rate1.setAgentRate(3);
			agentRates.add(rate1);
			
			agentRate rate2= (new setAgentRateBean()).new agentRate();
			rate2.setAgentType(list.get(1).getAgentType());
			rate2.setAgentRate(4);
			agentRates.add(rate2);
			
			bean.setMchId(channlAndAgent);
			bean.setAgentRateList(agentRates);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");
			list = agentTypeWapper.selectby(map);
			Logger.verifyEquals(rate1.getAgentRate().toString(), list.get(0).getRate().stripTrailingZeros().toPlainString(), "验证第一个代理类型agent_type表中的rate");
			Logger.verifyEquals(rate2.getAgentRate().toString(), list.get(1).getRate().stripTrailingZeros().toPlainString(), "验证第二个代理类型agent_type表中的rate");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_setAgentRate_005() {
		Logger.start(true,"正例-设置代理类型费率-渠道不是白名单");
		try {
			
			createmchBean createmchbean = GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			int userId = createmchbean.getMchId();
			
			Thread.sleep(2000);
			// 获取代理
			Map<String, Object> map = new HashMap();
			map.put("mchId", userId);
			List<agent_type> list = agentTypeWapper.selectby(map);
			
			List<agentRate> agentRates=new ArrayList();
			agentRate rate1= (new setAgentRateBean()).new agentRate();
			rate1.setAgentType(list.get(0).getAgentType());
			rate1.setAgentRate(3);
			agentRates.add(rate1);
			
			bean.setMchId(userId);
			bean.setAgentRateList(agentRates);
			call.setData(bean);
			call.callService();		
						
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("非白名单商户禁止设置代理分摊费率"), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_setAgentRate_006() {
		Logger.start(true,"正例-设置代理类型费率-代理类型id为空");
		try {
			// 获取代理
			Map<String, Object> map = new HashMap();
			map.put("mchId", channlNoAgent);
			List<agent_type> list = agentTypeWapper.selectby(map);
			
			agentRate rate= (new setAgentRateBean()).new agentRate();
			List<agentRate> agentRates=new ArrayList();
			
			//rate.setAgentType(list.get(0).getAgentType());
			rate.setAgentRate(3);
			agentRates.add(rate);
			
			bean.setMchId(channlNoAgent);
			bean.setAgentRateList(agentRates);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("参数异常"), "验证msg");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_setAgentRate_007() {
		Logger.start(true,"正例-设置代理类型费率-代理费率id为空");
		try {
			// 获取代理
			Map<String, Object> map = new HashMap();
			map.put("mchId", channlNoAgent);
			List<agent_type> list = agentTypeWapper.selectby(map);
			
			agentRate rate= (new setAgentRateBean()).new agentRate();
			List<agentRate> agentRates=new ArrayList();
			
			rate.setAgentType(list.get(0).getAgentType());
			//rate.setAgentRate(3);
			agentRates.add(rate);
			
			bean.setMchId(channlNoAgent);
			bean.setAgentRateList(agentRates);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("参数异常"), "验证msg");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_setAgentRate_008() {
		Logger.start(true,"正例-设置代理类型费率-代理费率 等于 0");
		try {
			// 获取代理
			Map<String, Object> map = new HashMap();
			map.put("mchId", channlNoAgent);
			List<agent_type> list = agentTypeWapper.selectby(map);
			
			agentRate rate= (new setAgentRateBean()).new agentRate();
			List<agentRate> agentRates=new ArrayList();
			
			rate.setAgentType(list.get(0).getAgentType());
			rate.setAgentRate(0);
			agentRates.add(rate);
			
			bean.setMchId(channlNoAgent);
			bean.setAgentRateList(agentRates);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");	
			list = agentTypeWapper.selectby(map);
			Logger.verifyEquals(rate.getAgentRate().toString(), list.get(0).getRate().stripTrailingZeros().toPlainString(), "验证第一个代理类型agent_type表中的rate");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}